package com.zyiot.lbs;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.Toast;
import cn.waps.AppConnect;
import cn.waps.MiniAdView;

import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;

public class MainActivity extends TabActivity {
	TabHost tabHost;
	TabHost.TabSpec tabSpec;
	RadioGroup radioGroup;
	RelativeLayout bottom_layout;
	ImageView img;
	int startLeft;
	Context context;
	private InitApplication app = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		app=(InitApplication) getApplication();
		this.context = this;
		bottom_layout = (RelativeLayout) findViewById(R.id.layout_bottom);
		Intent intent = getIntent();   
		//获取数据   
		String geotable_id = intent.getStringExtra("geotable_id");   
		String sortby = intent.getStringExtra("sortby");   
		app.setGeotable_id(geotable_id);
		app.setSortby(sortby);
		init();
		radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
		radioGroup.setOnCheckedChangeListener(checkedChangeListener);

		AppConnect.getInstance(context).setAdBackColor(Color.DKGRAY);// 设置迷你广告背景颜色
		AppConnect.getInstance(context).setAdForeColor(Color.WHITE);// 设置迷你广告文字颜色
		LinearLayout miniLayout = (LinearLayout) findViewById(R.id.miniAdLinearLayout);
		new MiniAdView(context, miniLayout).DisplayAd(10);// 10秒刷新一次

		// 自动更新UMENG
		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.update(this);

		// img = new ImageView(this);
		// img.setImageResource(R.drawable.tab_front_bg);
		// bottom_layout.addView(img);
	}

	private void init() {
		tabHost = getTabHost();
		setIndicator("msg", new Intent(this, TabMessagesActivity.class));
		setIndicator("list", new Intent(this, Test_TabListActivity.class));
		setIndicator("business", new Intent(this, TabBusinessActivity.class));
		setIndicator("more", new Intent(this, TabMoreActivity.class));
	}

	private void setIndicator(String title, Intent intent) {
		TabHost.TabSpec localTabSpec = tabHost.newTabSpec(title).setIndicator(title).setContent(intent);
		tabHost.addTab(localTabSpec);
	}

	private OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			case R.id.radio_home:
				if (CheckNetworkState()) {
					tabHost.setCurrentTabByTag("msg");
				}
				// ZyUnit.moveFrontBg(img, startLeft, 0, 0, 0);
				// startLeft = 0;
				break;
			case R.id.radio_list:
				if (CheckNetworkState()) {
					tabHost.setCurrentTabByTag("list");
				}
				// ZyUnit.moveFrontBg(img, startLeft, img.getWidth(), 0, 0);
				// startLeft = img.getWidth();
				break;
			case R.id.radio_bussiness:
				if (CheckNetworkState()) {
					tabHost.setCurrentTabByTag("business");
				}
				// ZyUnit.moveFrontBg(img, startLeft, img.getWidth() * 2, 0, 0);
				// startLeft = img.getWidth() * 2;
				break;
			case R.id.radio_more:
				if (CheckNetworkState()) {
					tabHost.setCurrentTabByTag("more");
				}
				// ZyUnit.moveFrontBg(img, startLeft, img.getWidth() * 3, 0, 0);
				// startLeft = img.getWidth() * 3;
				break;
			default:
				break;
			}
		}
	};

	/*
	 * 添加对back按钮的处理，点击提示退出 (non-Javadoc)
	 * 
	 * @see android.app.Activity#dispatchKeyEvent(android.view.KeyEvent)
	 */
	private long exitTime = 0;

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			if ((System.currentTimeMillis() - exitTime) > 2000) {
				Toast.makeText(getApplicationContext(), R.string.exit_msg_toast, Toast.LENGTH_SHORT).show();
				exitTime = System.currentTimeMillis();
			} else {
				finish();
				AppConnect.getInstance(this).close();
				android.os.Process.killProcess(android.os.Process.myPid());
			}
			return true;
		}
		return super.dispatchKeyEvent(event);
	}

	public boolean CheckNetworkState() {
		boolean flag = false;
		ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (manager.getActiveNetworkInfo() != null) {
			flag = manager.getActiveNetworkInfo().isAvailable();
		}
		if (!flag) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setIcon(android.R.drawable.ic_dialog_alert);
			builder.setTitle(R.string.netstate);
			builder.setMessage(R.string.setnetwork);
			builder.setPositiveButton(R.string.setbutton, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Intent intent;
					if (android.os.Build.VERSION.SDK_INT > 10) {
						startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
					} else {
						startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
					}
				}
			});
			builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
					android.os.Process.killProcess(android.os.Process.myPid());
				}
			});
			builder.create();
			builder.show();
		}

		return flag;
	}

	@Override
	protected void onResume() {
		CheckNetworkState();
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}